package com.linkoog.devtools.http.linemarker.action;

import com.intellij.openapi.actionSystem.ActionUpdateThread;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiMethod;
import com.linkoog.devtools.http.raw_index.HttpServiceMethod;
import com.linkoog.devtools.http.raw_index.HttpServiceMethodRawFileIndex;
import com.linkoog.devtools.http.toolwindow.HttpToolWindowManager;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;

import static com.intellij.openapi.actionSystem.CommonDataKeys.EDITOR;
import static com.intellij.openapi.actionSystem.CommonDataKeys.PROJECT;


public class HttpQuickTestAction extends AnAction {

    private final PsiMethod psiMethod;

    public HttpQuickTestAction(@NotNull PsiMethod psiMethod, @NotNull String text, String description, Icon icon) {
        super(text, description, icon);
        this.psiMethod = psiMethod;
    }


    @Override
    public void actionPerformed(AnActionEvent e) {
        Project project = e.getData(CommonDataKeys.PROJECT);
        if (project == null) return;

        final HttpServiceMethodRawFileIndex httpServiceMethodRawFileIndex = HttpServiceMethodRawFileIndex.getInstance(project);
        HttpServiceMethod httpServiceMethod = httpServiceMethodRawFileIndex.getCache(psiMethod);
        if (httpServiceMethod == null) return;

        HttpToolWindowManager.getInstance(project).show(httpServiceMethod);
    }

    @Override
    public void update(AnActionEvent e) {
        Project project = e.getData(PROJECT);
        Editor editor = e.getData(EDITOR);
        e.getPresentation().setVisible(project != null && editor != null);
    }

    @Override
    public @NotNull ActionUpdateThread getActionUpdateThread() {
        return ActionUpdateThread.BGT;
    }


}
